package pl.edu.pjwstk.kor.sbql4j.project3;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.*;
import com.db4o.ObjectContainer;

import com.db4o.foundation.*;

import com.db4o.internal.*;
import com.db4o.internal.btree.*;

import org.apache.commons.collections.CollectionUtils;

import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;
import pl.edu.pjwstk.kor.model.Service;

import pl.wcislo.sbql4j.exception.*;
import pl.wcislo.sbql4j.java.model.runtime.*;
import pl.wcislo.sbql4j.java.model.runtime.Struct;
import pl.wcislo.sbql4j.java.model.runtime.factory.*;
import pl.wcislo.sbql4j.java.utils.ArrayUtils;
import pl.wcislo.sbql4j.java.utils.OperatorUtils;
import pl.wcislo.sbql4j.java.utils.Pair;
import pl.wcislo.sbql4j.lang.codegen.nostacks.*;
import pl.wcislo.sbql4j.lang.codegen.simple.*;




import pl.wcislo.sbql4j.lang.parser.expression.*;
import pl.wcislo.sbql4j.lang.parser.expression.OrderByParamExpression.SortType;
import pl.wcislo.sbql4j.lang.parser.terminals.*;
import pl.wcislo.sbql4j.lang.parser.terminals.operators.*;
import pl.wcislo.sbql4j.lang.types.*;
import pl.wcislo.sbql4j.lang.xml.*;
import pl.wcislo.sbql4j.model.*;
import pl.wcislo.sbql4j.model.collections.*;
import pl.wcislo.sbql4j.util.*;
import pl.wcislo.sbql4j.util.Utils;
import pl.wcislo.sbql4j.xml.model.*;
import pl.wcislo.sbql4j.xml.parser.store.*;

import java.util.*;
import java.util.Collection;


public class Project3Query_SbqlQuery5Db4o0 implements Db4oSBQLQuery {
    public Project3Query_SbqlQuery5Db4o0() {
    }

    /**
     * query='db.( avg(Employee.getDeals().getCost()) group as _aux0).(Employee where  avg(getDeals().getCost()) < _aux0)'
    '
     **/
    public java.util.Collection<pl.edu.pjwstk.kor.model.Employee> executeQuery(
        final ObjectContainerBase ocb, final Transaction t) {
        final LocalTransaction transLocal = (LocalTransaction) t;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Employee> _ident_Employee =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Employee>();
        ClassMetadata _classMeta10 = ocb.classCollection()
                                        .getClassMetadata("pl.edu.pjwstk.kor.model.Employee");
        long[] _ids10 = _classMeta10.getIDs(transLocal);

        for (long _id10 : _ids10) {
            LazyObjectReference _ref10 = transLocal.lazyReferenceFor((int) _id10);
            _ident_Employee.add((pl.edu.pjwstk.kor.model.Employee) _ref10.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Deal> _dotResult = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Deal>();
        int _dotIndex = 0;

        for (pl.edu.pjwstk.kor.model.Employee _dotEl : _ident_Employee) {
            if (_dotEl == null) {
                continue;
            }

            if (_dotEl != null) {
                ocb.activate(_dotEl, 1);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Deal> _mth_getDealsResult = _dotEl.getDeals();

            if (_mth_getDealsResult != null) {
                ocb.activate(_mth_getDealsResult, 2);
            }

            if (_mth_getDealsResult != null) {
                ocb.activate(_mth_getDealsResult, 2);
            }

            _dotResult.addAll(_mth_getDealsResult);
            _dotIndex++;
        }

        java.util.Collection<java.lang.Double> _dotResult1 = new java.util.ArrayList<java.lang.Double>();
        int _dotIndex1 = 0;

        for (pl.edu.pjwstk.kor.model.Deal _dotEl1 : _dotResult) {
            if (_dotEl1 == null) {
                continue;
            }

            if (_dotEl1 != null) {
                ocb.activate(_dotEl1, 1);
            }

            java.lang.Double _mth_getCostResult = _dotEl1.getCost();

            if (_mth_getCostResult != null) {
                ocb.activate(_mth_getCostResult, 1);
            }

            if (_mth_getCostResult != null) {
                ocb.activate(_mth_getCostResult, 1);
            }

            _dotResult1.add(_mth_getCostResult);
            _dotIndex1++;
        }

        java.lang.Double _avgResult = 0d;

        if ((_dotResult1 != null) && !_dotResult1.isEmpty()) {
            Number _avgSum1 = null;

            for (Number _avgEl1 : _dotResult1) {
                _avgSum1 = MathUtils.sum(_avgSum1, _avgEl1);
            }

            _avgResult = _avgSum1.doubleValue() / _dotResult1.size();
        }

        java.lang.Double _groupAsResult_aux0 = _avgResult;
        java.lang.Double _dotEl3 = _groupAsResult_aux0;
        final java.util.Collection<pl.edu.pjwstk.kor.model.Employee> _ident_Employee1 =
            new java.util.ArrayList<pl.edu.pjwstk.kor.model.Employee>();
        ClassMetadata _classMeta11 = ocb.classCollection()
                                        .getClassMetadata("pl.edu.pjwstk.kor.model.Employee");
        long[] _ids11 = _classMeta11.getIDs(transLocal);

        for (long _id11 : _ids11) {
            LazyObjectReference _ref11 = transLocal.lazyReferenceFor((int) _id11);
            _ident_Employee1.add((pl.edu.pjwstk.kor.model.Employee) _ref11.getObject());
        }

        java.util.Collection<pl.edu.pjwstk.kor.model.Employee> _whereResult = new java.util.ArrayList<pl.edu.pjwstk.kor.model.Employee>();
        int _whereLoopIndex = 0;

        for (pl.edu.pjwstk.kor.model.Employee _whereEl : _ident_Employee1) {
            if (_whereEl == null) {
                continue;
            }

            if (_whereEl != null) {
                ocb.activate(_whereEl, 1);
            }

            java.util.List<pl.edu.pjwstk.kor.model.Deal> _mth_getDealsResult1 = _whereEl.getDeals();

            if (_mth_getDealsResult1 != null) {
                ocb.activate(_mth_getDealsResult1, 2);
            }

            java.util.List<java.lang.Double> _dotResult2 = new java.util.ArrayList<java.lang.Double>();
            int _dotIndex2 = 0;

            for (pl.edu.pjwstk.kor.model.Deal _dotEl2 : _mth_getDealsResult1) {
                if (_dotEl2 == null) {
                    continue;
                }

                if (_dotEl2 != null) {
                    ocb.activate(_dotEl2, 1);
                }

                java.lang.Double _mth_getCostResult1 = _dotEl2.getCost();

                if (_mth_getCostResult1 != null) {
                    ocb.activate(_mth_getCostResult1, 1);
                }

                if (_mth_getCostResult1 != null) {
                    ocb.activate(_mth_getCostResult1, 1);
                }

                _dotResult2.add(_mth_getCostResult1);
                _dotIndex2++;
            }

            java.lang.Double _avgResult1 = 0d;

            if ((_dotResult2 != null) && !_dotResult2.isEmpty()) {
                Number _avgSum2 = null;

                for (Number _avgEl2 : _dotResult2) {
                    _avgSum2 = MathUtils.sum(_avgSum2, _avgEl2);
                }

                _avgResult1 = _avgSum2.doubleValue() / _dotResult2.size();
            }

            java.lang.Double _ident__aux0 = _dotEl3;

            if (_ident__aux0 != null) {
                ocb.activate(_ident__aux0, 1);
            }

            Boolean _lessResult = _avgResult1 < _ident__aux0;

            if (_lessResult) {
                _whereResult.add(_whereEl);
            }

            _whereLoopIndex++;
        }

        pl.wcislo.sbql4j.db4o.utils.DerefUtils.activateResult(_whereResult, ocb);

        return _whereResult;
    }
}
